from pymilvus import connections, utility

# 配置连接参数
MILVUS_HOST = "10.0.54.30"
MILVUS_PORT = "31540"  # 注意：端口号应该是整数，但新版本支持字符串格式
USERNAME = "root"
PASSWORD = "hjsdg@db50_GdU43hj"

try:
    # 建立连接
    connections.connect(
        alias="default",
        host=MILVUS_HOST,
        port=MILVUS_PORT,  # 如果报错，尝试转换为整数 int(MILVUS_PORT)
        user=USERNAME,
        password=PASSWORD
    )
    
    # 验证连接
    print("连接成功！版本信息：", utility.get_server_version())
    
    # 查看现有集合
    collections = utility.list_collections()
    print(f"现有集合：{collections}")

except Exception as e:
    print("连接失败，错误信息：", e)

# db_name="default"
# client = MilvusClient(uri="http://10.240.1.3:19530", db_name=db_name)
# COLLECTION_NAME = "NNU_user"
# import time
# s_t = time.time()
# get_collection = Collection(COLLECTION_NAME, using="default")
# # get_collection.load()
# res = get_collection.query(

#     # collection_name=COLLECTION_NAME,
#     expr="USER_ID == 293064",
#     # ids=["e9aa490229f5f29b90baa5228bc306a9", "35e3debf8554de1c0939eb0db3554b72"],
#     output_fields=["embedding"]
# )
# # get_collection.release()
# e_t = time.time()
# print("time cost", e_t - s_t)
# print(res)